package cn.com.bluemoon.daps.model.mapper;

import cn.com.bluemoon.daps.model.entity.DapDataModelRecord;
import cn.com.bluemoon.daps.model.vo.SearchModelApplicationVo;
import cn.com.bluemoon.daps.model.vo.SearchModelVersionVo;
import cn.com.bluemoon.daps.standard.dto.ApplicationModelDto;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

/**
 * <p>
 * 申请发布审批状态表 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2020-08-03
 */
@Repository
public interface DapDataModelRecordMapper extends BaseMapper<DapDataModelRecord> {

    /**
     * 申请发布列表
     * @param page
     * @param application
     * @return
     */
    Page<ApplicationModelDto> selectApplicationList(Page page,
                                                    @Param("application") SearchModelApplicationVo application,
                                                    @Param("applicationType") Integer applicationType);
    /**
     * 将表数据置为发布状态
     * @param modelId
     */
    void updateApplicationStatus(@Param("modelId") String modelId);

    /**
     * 更新发布时间
     * @param record
     */
    @Update("update dap_data_model_record as a left join dap_data_model_operate_history as b on\n" +
            "        a.id = b.data_model_record_id\n" +
            "        set\n" +
            "        a.release_time = current_timestamp(),\n" +
            "        a.public_status = 1,\n" +
            "        a.verify_by = #{verifyBy},\n" +
            "        b.public_time = current_timestamp()\n" +
            "        where a.id = #{record}")
    void updatePublicTime(@Param("record") String record, @Param("verifyBy") String verifyBy);

    /**
     * @param sysId 系统id
     * @return 获取系统下所有模型中最新的已发布时间
     */
    @Select("select\n" +
            "max(a.release_time) as release_time\n" +
            "from dap_data_model_record as a\n" +
            "left join dap_data_model b on a.model_id =b.id and b.bm_status =1\n" +
            "left join dap_system_info c on c.id = b.system_id and c.bm_status =1\n" +
            "where a.public_status=1\n" +
            "and c.id =#{sysId}  and a.bm_status=1")
    LocalDateTime findMaxReleaseTimeBySystemId(@Param("sysId") String sysId);

    /**
     * 版本列表
     * @param page
     * @param version
     * @return
     */
    Page<DapDataModelRecord> selectModelVersionList(Page page, @Param("version")SearchModelVersionVo version);
}
